package com.jy.modules.cims.contract.service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.exception.CimsException;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.service.TCMmCreditPackageService;
import com.jy.modules.cims.component.contract.ICContract;
import com.jy.modules.cims.data.common.dao.TCMmAgreementDao;
import com.jy.modules.cims.data.common.dao.TCMmContractConstraintDao;
import com.jy.modules.cims.data.common.dao.TCMmContractDao;
import com.jy.modules.cims.data.common.dao.TCMmCooperorgDao;
import com.jy.modules.cims.data.common.dao.TCMmCreditPackageDao;
import com.jy.modules.cims.data.common.dao.TCMmCreditSettleParamDao;
import com.jy.modules.cims.data.common.dao.TCMmFeeSettleParamDao;
import com.jy.modules.cims.data.common.dto.TCMmAgreementDTO;
import com.jy.modules.cims.data.common.dto.TCMmContractConstraintDTO;
import com.jy.modules.cims.data.common.dto.TCMmContractDTO;
import com.jy.modules.cims.data.common.dto.TCMmCooperorgDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditPackageDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditSettleParamDTO;
import com.jy.modules.cims.data.common.dto.TCMmFeeSettleParamDTO;
import com.jy.modules.cims.data.dto.AgreementDto;
import com.jy.modules.cims.data.dto.AgreementQueryDto;
import com.jy.modules.cims.data.dto.ContractAllDto;
import com.jy.modules.cims.data.dto.ContractDto;
import com.jy.modules.cims.data.dto.ContractQueryDto;
import com.jy.modules.cims.data.dto.CooperOrgDto;
import com.jy.modules.cims.data.dto.CooperOrgQueryDto;
import com.jy.modules.cims.data.dto.DealDto;
import com.jy.modules.cims.deal.service.DealService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.exception.AbaboonException;

/**
 * 合约处理服务层
 */
@Service("com.jy.modules.cims.contract.service.ContractService")
public class ContractService {

	@Autowired
	@Qualifier("com.jy.modules.cims.deal.service.DealService")
	private DealService dealService;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmCreditPackageService")
	private TCMmCreditPackageService creditPackageService;

	@Autowired
	private TCMmContractDao cdao;

	@Autowired
	private TCMmFeeSettleParamDao fspdao;

	@Autowired
	private TCMmCreditSettleParamDao cspdao;

	@Autowired
	private TCMmContractConstraintDao ccdao;

	@Autowired
	private TCMmAgreementDao agreementDao;

	@Autowired
	private TCMmCooperorgDao cooperorgDao;

	@Autowired
	public TCMmCreditPackageDao creditPackageDao;

	/**
	 * 合约处理服务层
	 */
	public ContractService() {
	}

	/**
     * 
     */
	public ICContract iCContract;

	/**
	 * 根据查询参数返回合作机构列表
	 * 
	 * @param dto
	 * @return
	 */
	public List<CooperOrgDto> getCooperOrgList(CooperOrgQueryDto dto) {
		// TODO implement here
		return null;
	}

	/**
	 * 协议查询列表
	 * 
	 * @param dto
	 * @return
	 */
	public List<AgreementDto> getAgreementList(AgreementQueryDto dto) {
		// TODO implement here
		return null;
	}

	/**
	 * 合约查询列表
	 * 
	 * @param dto
	 * @return
	 */
	public List<ContractDto> getContractList(ContractQueryDto dto) {
		// TODO implement here
		return null;
	}

	/**
	 * 获取合约综合信息
	 * 
	 * @param contractId
	 * @return
	 */
	public ContractAllDto getContractAllInfo(String contractId) {

		if (SimpleStringUtils.isBlank(contractId)) {
			return null;
		}

		ContractAllDto conAllDto = new ContractAllDto();
		Map<String, Object> map = new HashMap<String, Object>();

		// 合约基本信息
		TCMmContractDTO cdto = new TCMmContractDTO();
		cdto.setContractCode(contractId);
		map.put("dto", cdto);
		List<TCMmContractDTO> cdtolist = cdao.searchTCMmContract(map);
		if (cdtolist.size() > 0) {
			cdto = cdtolist.get(0);
			conAllDto.setCdto(cdto);
		}

		// 合约费用参数
		List<TCMmFeeSettleParamDTO> fspDtolist = new ArrayList<TCMmFeeSettleParamDTO>();
		map.clear();
		TCMmFeeSettleParamDTO fspdto = new TCMmFeeSettleParamDTO();
		fspdto.setContractCode(contractId);
		map.put("dto", fspdto);
		fspDtolist = fspdao.searchTCMmFeeSettleParam(map);
		conAllDto.setFspDtolist(fspDtolist);

		// 债权结算参数
		TCMmCreditSettleParamDTO cspDto = new TCMmCreditSettleParamDTO();
		List<TCMmCreditSettleParamDTO> cspDtolist = new ArrayList<TCMmCreditSettleParamDTO>();
		map.clear();
		cspDto.setContractCode(contractId);
		map.put("dto", cspDto);
		cspDtolist = cspdao.searchTCMmCreditSettleParam(map);
		if (cspDtolist.size() > 0) {
			cspDto = cspDtolist.get(0);
			conAllDto.setCspDto(cspDto);
		}

		// 合约债权约束
		List<TCMmContractConstraintDTO> ccDtolist = new ArrayList<TCMmContractConstraintDTO>();
		map.clear();
		TCMmContractConstraintDTO ccdto = new TCMmContractConstraintDTO();
		ccdto.setContractCode(contractId);
		map.put("dto", ccdto);
		ccDtolist = ccdao.searchTCMmContractConstraint(map);
		conAllDto.setCcDtolist(ccDtolist);

		if (!SimpleStringUtils.isBlank(cdto.getAgreementCode())) {
			// 协议信息
			List<TCMmAgreementDTO> agreementDTOlist = new ArrayList<TCMmAgreementDTO>();
			map.clear();
			TCMmAgreementDTO agreementDTO = new TCMmAgreementDTO();
			agreementDTO.setAgreementCode(cdto.getAgreementCode());
			map.put("dto", agreementDTO);
			agreementDTOlist = agreementDao.searchTCMmAgreement(map);
			if (agreementDTOlist.size() > 0) {
				conAllDto.setAgreementDTO(agreementDTOlist.get(0));
			}
		}

		if (!SimpleStringUtils.isBlank(cdto.getCooperorgCode())) {
			// 合作机构信息
			List<TCMmCooperorgDTO> cooperorgDTOlist = new ArrayList<TCMmCooperorgDTO>();
			map.clear();
			TCMmCooperorgDTO cooperorgDTO = new TCMmCooperorgDTO();
			cooperorgDTO.setCooperorgCode(cdto.getCooperorgCode());
			map.put("dto", cooperorgDTO);
			cooperorgDTOlist = cooperorgDao.searchTCMmCooperorg(map);
			if (cooperorgDTOlist.size() > 0) {
				conAllDto.setCooperorgDTO(cooperorgDTOlist.get(0));
			}
		}

		//
		List<TCMmCreditPackageDTO> creditPackageList = new ArrayList<TCMmCreditPackageDTO>();
		map.clear();
		TCMmCreditPackageDTO creditPackage = new TCMmCreditPackageDTO();
		creditPackage.setCooperorgCode(cdto.getCooperorgCode());
		creditPackage.setContractCode(cdto.getContractCode());
		map.put("dto", creditPackage);
		creditPackageList = creditPackageDao.searchTCMmCreditPackage(map);
		if (CollectionsUtils.isNotEmpty(creditPackageList)) {
			conAllDto.setCreditPackageList(creditPackageList);
		}
		return conAllDto;
	}

	/**
	 * @author dell
	 * @description:通过合约编号查询 其明细信息
	 * @date 2015-01-06 11:21:31
	 * @param id
	 * @return
	 */
	public ModelAndView queryAllDTO(String contractCode) throws AbaboonException {
		ModelAndView model = new ModelAndView();
		try {
			ContractAllDto dto = this.getContractAllInfo(contractCode);
			// 将信息放入 model 以便于传至页面 使用
			model.addObject("dto", dto);
			model.addObject("contractAllDto", dto);
		} catch (Exception e) {
			throw new AbaboonException("执行queryOneDTO异常：", e);
		}
		return model;
	}

	/**
	 * 返回合约
	 * 
	 * @param contractCode
	 * @return
	 * @throws AbaboonException
	 */
	public List<ContractAllDto> returnContract(String contractCode) throws AbaboonException {
		List<ContractAllDto> contracts = new ArrayList<ContractAllDto>();
		try {
			ContractAllDto dto = this.getContractAllInfo(contractCode);
			contracts.add(dto);
		} catch (Exception e) {
			throw new AbaboonException("执行returnContract异常：", e);
		}
		return contracts;
	}

	/**
	 * @author dell
	 * @description:通过合约编号查询 其明细信息
	 * @date 2015-01-06 11:21:31
	 * @param id
	 * @return
	 */
	public ModelAndView queryAllDTO(String cooperorgCode, String contractCode, String dealCode) throws AbaboonException {
		ModelAndView model = new ModelAndView();
		try {
			ContractAllDto dto = this.getContractAllInfo(contractCode);
			Map<String, Object> searchParams = new HashMap<String, Object>();
			DealDto dealDto = new DealDto();
			dealDto.setDealCode(dealCode);
			searchParams.put("dto", dealDto);
			QueryReqBean params = new QueryReqBean();
			params.setSearchParams(searchParams);
			PageParameter pageInfo = PageUtil.toPageParameter(new DataMsg());
			params.setPageParameter(pageInfo);
			List<DealDto> dealDtoList = dealService.getDealList(params.getSearchParams());
			
			model.addObject("dealDto", CollectionsUtils.isEmpty(dealDtoList) ? null : dealDtoList.get(0));
			
			// 将信息放入 model 以便于传至页面 使用
			model.addObject("dto", dto);
			model.addObject("contractAllDto", dto);
			model.addObject("dealCode", dealCode);
		} catch (Exception e) {
			throw new AbaboonException("执行queryOneDTO异常：", e);
		}
		return model;
	}
	
	public List<TCMmCreditPackageDTO> dealPackages(String cooperorgCode, String contractCode, String creditPackageName) throws AbaboonException {
		List<TCMmCreditPackageDTO> list = null;
		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			TCMmCreditPackageDTO dto = new TCMmCreditPackageDTO();
			dto.setContractCode(contractCode);
			dto.setCooperorgCode(cooperorgCode);
			dto.setCreditPackageName(creditPackageName);
			searchParams.put("dto", dto);
			list = creditPackageService.searchTCMmCreditPackage(searchParams);
			
			
		} catch (Exception e) {
			throw new AbaboonException("执行dealPackages异常：", e);
		}
		return list;
	}

	/**
	 * @description 验证债权结算参数
	 * @param contractCode
	 *            合约编号
	 * @author shengzhoutao
	 * @date 2015-08-26
	 */
	public void checkCreditSettleParam(final String contractCode) {
		TCMmCreditSettleParamDTO creditSettleParamDTO = new TCMmCreditSettleParamDTO();
		creditSettleParamDTO.setContractCode(contractCode);
		List<TCMmCreditSettleParamDTO> creditSettleParamDTOList = cspdao.searchTCMmCreditSettleParam(SearchParamUtil
				.getSearchParam(creditSettleParamDTO));
		creditSettleParamDTO = null;
		if (CollectionsUtils.isEmpty(creditSettleParamDTOList)) {
			throw new CimsException(MessageFormat.format("根据合约编号[{0}]没有查询到债权结算参数记录！", new Object[] { contractCode }));
		}

		String settleType = creditSettleParamDTOList.get(0).getSettleType();
		if (!SimpleStringUtils.hasText(settleType)) {
			throw new CimsException(MessageFormat.format("合约编号[{0}]债权结算参数的结算计算方式(01:借款人结算方式;02:等额本息;03:按月付息)没有配置！", new Object[] { contractCode }));
		}
		String replaceType = creditSettleParamDTOList.get(0).getReplaceType();
		if (!SimpleStringUtils.hasText(replaceType)) {
			throw new CimsException(MessageFormat.format("合约编号[{0}]债权结算参数的月还处理方式(01:全额匹配;02:本金匹配;03:全额返还)没有配置！", new Object[] { contractCode }));
		}
	}
	/**
	 * 
	 * @Description 根据合约编号查询，结算参数列表
	 * @param contractCode
	 * @return
	 * @throws Exception   
	 * List<TCMmCreditSettleParamDTO>  
	 * @throws
	 * @author zhangmi
	 * @date 2016-3-4
	 */
	public List<TCMmCreditSettleParamDTO> getCreditSettleList(final String contractCode) throws Exception{
		TCMmCreditSettleParamDTO creditSettleParamDTO = new TCMmCreditSettleParamDTO();
		creditSettleParamDTO.setContractCode(contractCode);
		List<TCMmCreditSettleParamDTO> creditSettleParamDTOList = cspdao.searchTCMmCreditSettleParam(SearchParamUtil
				.getSearchParam(creditSettleParamDTO));
		if(CollectionsUtils.isEmpty(creditSettleParamDTOList)){
			throw new CimsException(MessageFormat.format("根据合约编号[{0}]没有查询到债权结算参数记录！", new Object[] { contractCode }));
		}
		return creditSettleParamDTOList;
	}
}